package org.hncors.gnssparse;


import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.southgnss.connect.DeviceParManage;
import com.southgnss.connect.SateEnableEvent;
import com.southgnss.network.CorsClientManage;
import com.southgnss.network.NetConfigNtripEvent;

import org.hncors.gnssparse.SelectTemplateDialog;
import org.hncors.gnssparse.base.BaseActivity;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by PC8681 on 2018/1/5.
 */
public class SettingPageRtkManagerNetConfigBluetooth extends BaseActivity implements View.OnClickListener
        , SelectTemplateDialog.onCustomDialogSelectedListener
        , SelectTemplateWithAcceptPointRefreshDialog.onCustomDialogWithAcceptPointRefreshListener {
    private EditText mIpEditText;
    private EditText mPorEditText;
    private EditText mUserEditText;
    private EditText mPasswdEditText;
    private TextView mAcceptTextView;
    private TextView mModeTextView;

    private Button mConnectButton;
    private Button mDisconnectButton;
    private Button gpsButton, sbasButton, gpsANDsbasButton;

    private DeviceParManage dPM;
    private String workMode;
    private String mountPoint;
    private String ip;
    private String port;
    private String user;
    private String pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        appendMainBody(this, R.layout.layout_setting_net_config_bluetooth);
        appendTopBody(R.layout.activity_top_text);
        setTopBarTitle("连接设置");
        // 初始化
        InitData();
        InitUI();
    }


    private void InitData() {
        EventBus.getDefault().register(this);
        dPM = DeviceParManage.GetInstance();
        //设置数据链移动网络，就是盒子插卡
        dPM.SetCurDataLink("CELLULAR_NET");
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    private void InitUI() {
        View acceptView = findViewById(R.id.layoutSettingRtkNetConfigAcceptPoint);
        if (acceptView != null) acceptView.setOnClickListener(this);
        View modeView = findViewById(R.id.layoutSettingRtkNetConfigMode);
        if (modeView != null) modeView.setOnClickListener(this);

        mIpEditText = (EditText) findViewById(R.id.editTextSettingRtkNetConfigIpContent);
        mPorEditText = (EditText) findViewById(R.id.editTextSettingRtkNetConfigPortContent);
        mUserEditText = (EditText) findViewById(R.id.editTextSettingRtkNetConfigUserContent);
        mPasswdEditText = (EditText) findViewById(R.id.editTextSettingRtkNetConfigPasswordContent);

        mAcceptTextView = (TextView) findViewById(R.id.textViewSettingRtkNetConfigAcceptPointContent);
        mModeTextView = (TextView)findViewById(R.id.textViewSettingRtkNetConfigModeContent);

        mConnectButton = (Button) findViewById(R.id.buttonConnect);
        mDisconnectButton = (Button) findViewById(R.id.buttonDisConnect);
        gpsButton = (Button) findViewById(R.id.buttonGps);
        sbasButton = (Button) findViewById(R.id.buttonbeidou);
        gpsANDsbasButton = (Button) findViewById(R.id.buttonGpsAndbeidou);
        if (mConnectButton != null) mConnectButton.setOnClickListener(this);
        if (mDisconnectButton != null) mDisconnectButton.setOnClickListener(this);
        if (gpsButton != null) gpsButton.setOnClickListener(this);
        if (sbasButton != null) sbasButton.setOnClickListener(this);
        if (gpsANDsbasButton != null) gpsANDsbasButton.setOnClickListener(this);


        ip = dPM.GetWorkParaIP();
        port = String.valueOf(dPM.GetWorkParaPort());
        user = dPM.GetWorkParaUserName();
        pass = dPM.GetWorkParaPassword();
        mountPoint= dPM.GetWorkParaMountPoint();
        workMode = dPM.GetWorkParaMode();

        mIpEditText.setText(ip);
        mPorEditText.setText(port);
        mUserEditText.setText(user);
        mPasswdEditText.setText(pass);
        mAcceptTextView.setText(mountPoint);
        mModeTextView.setText(workMode);
    }

    @Override
    public void onClick(View arg0) {
        if (arg0.getId() == R.id.layoutSettingRtkNetConfigAcceptPoint) {
            //点击接入点
            setApnSomeParam();
            ArrayList<String> listdata = GetNetConfigAcceptPointList();
            SelectTemplateWithAcceptPointRefreshDialog dialog = SelectTemplateWithAcceptPointRefreshDialog.newInstance(
                    getString(R.string.setting_rtk_net_config_accept_point),
                    listdata,
                    -1,
                    5);
            dialog.show(getSupportFragmentManager(), "AcceptDialog");
        } else if (arg0.getId() == R.id.buttonConnect) {
            //设置参数，成功后eventbus返回消息
            setApnSomeParam();
        } else if (arg0.getId() == R.id.buttonDisConnect) {
            //断开，成功后eventbus返回消息
            dPM.SetNTPIPConnectStart(false);
        } else if (arg0.getId() == R.id.buttonGps) {
            //单GPS切换，其他卫星关闭，成功后eventbus返回消息
            dPM.SetSatelliteEnableGps(true);
            dPM.SetSatelliteEnableBds(false);
            dPM.SetSatelliteEnableGalileo(false);
            dPM.SetSatelliteEnableGlonass(false);
            dPM.SetSatelliteEnableQzss(false);
            dPM.SetSatelliteEnableSbas(false);
        } else if (arg0.getId() == R.id.buttonbeidou) {
            //单北斗切换，其他卫星关闭，成功后eventbus返回消息
            dPM.SetSatelliteEnableBds(true);
            dPM.SetSatelliteEnableSbas(false);
            dPM.SetSatelliteEnableGps(false);
            dPM.SetSatelliteEnableGalileo(false);
            dPM.SetSatelliteEnableGlonass(false);
            dPM.SetSatelliteEnableQzss(false);
        } else if (arg0.getId() == R.id.buttonGpsAndbeidou) {
            //两者（GPS，北斗）混合定位切换，其他卫星关闭，成功后eventbus返回消息
            dPM.SetSatelliteEnableGps(true);
            dPM.SetSatelliteEnableBds(true);
            dPM.SetSatelliteEnableSbas(false);
            dPM.SetSatelliteEnableGalileo(false);
            dPM.SetSatelliteEnableGlonass(false);
            dPM.SetSatelliteEnableQzss(false);
        } else if (arg0.getId() == R.id.layoutSettingRtkNetConfigMode) {
            //点击模式
            ArrayList<String> listdata = GetNetConfigModeList();
            if (workMode == null || workMode.equals("") && listdata != null && !listdata.isEmpty()) {
                workMode = listdata.get(0);
            }
            SelectTemplateDialog selectDialog = SelectTemplateDialog.newInstance(
                    getString(R.string.setting_rtk_net_config_mode),
                    listdata, listdata.indexOf(workMode), 4);
            selectDialog.show(getSupportFragmentManager(), "SelectDialog");
        }
    }

    //获取选择的模式并显示
    @Override
    public void onCustomDialogSingleSelectedListener(int uniqueIdentifier,
                                                     int selectIndex, ArrayList<String> data) {
    if (uniqueIdentifier == 4) {
            //模式
             workMode= data.get(selectIndex);
            if (mModeTextView != null) mModeTextView.setText(workMode);
        }
    }

    /**
     * 获取当前的配置网络模式列表
     */
    private ArrayList<String> GetNetConfigModeList() {
        ArrayList<String> list = new ArrayList<String>();
        list.add("NTRIP");
        list.add("SOUTH");
        list.add("TCPIP");
        return list;
    }

    /**
     * 获取当前的接入点列表
     */
    private ArrayList<String> GetNetConfigAcceptPointList() {
        return dPM.GetMountPointList();
    }

    // 设置移动网络的相关参数
    private boolean setApnSomeParam() {
        ip = mIpEditText.getText().toString();
        port = mPorEditText.getText().toString();
        user = mUserEditText.getText().toString();
        pass = mPasswdEditText.getText().toString();
        mountPoint= mAcceptTextView.getText().toString();
        workMode = mModeTextView.getText().toString();
        if (ip.length() == 0) {
            Toast.makeText(this, getString(R.string.IpCanNotIsNull), Toast.LENGTH_SHORT).show();
            return false;
        }
        if (port.length() == 0) {
            Toast.makeText(this, getString(R.string.PortCanNotIsNull), Toast.LENGTH_SHORT).show();
            return false;
        }
        ProgramConfigWrapper.GetInstance(this).SetNTRIPPara(
                ip, Integer.valueOf(port), user, pass, mountPoint);

        if (ip != null && port != null & user != null && pass != null) {
            dPM.SetWorkPara(workMode, ip, Integer.parseInt(port), user, pass, mountPoint);
        }
        return true;
    }

    //列表接入点接入点对话框返回
    @Override
    public void onCustomDialogWithAcceptPointSelectListener(
            int uniqueIdentifier, int selectIndex, ArrayList<String> data) {
        mountPoint = data.get(selectIndex);
        mAcceptTextView.setText(mountPoint);
    }

    //刷新接入点对话框返回
    @Override
    public void onCustomDialogWithAcceptPointRefreshPleaseListener() {
        // 首先是设置基本参数
        dPM.SetUpdateMountPoint();
    }

    //输入接入点对话框返回
    @Override
    public void onCustomDIalogWithAcceptPointEditInputListener(
            int uniqueIdentifier, String data) {
        if (data.length() == 0) return;
        mountPoint = data;
        mAcceptTextView.setText(mountPoint);
    }

    /**************************************************************************
     * 下面的是设置参数的成功与否的 事件通知
     *************************************************************************/
    //刷新接入点返回消息
    public void onEventMainThread(NetConfigNtripEvent.DeviceNTRIPAcceptPointEvent obj) {
        if (obj == null) return;
        if (obj.getStatus() == true) {
            Toast.makeText(this, getString(R.string.AcceptPointListSuccess), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, getString(R.string.AcceptPointListFailed), Toast.LENGTH_SHORT).show();
        }
    }
    //设置参数返回消息 就是设置ip 端口 用户名密码等
    public void onEventMainThread(NetConfigNtripEvent.DeviceNTRIPWorkParaEvent obj) {
        if (obj == null) return;
        if (obj.getStatus() == 0) {
            dPM.SetNTPIPConnectStart(true);
//            Toast.makeText(this, getString(R.string.setting_rtk_net_setting_success), Toast.LENGTH_SHORT).show();
        } else if(obj.getStatus() == 1){
            Toast.makeText(this, getString(R.string.setting_rtk_net_setting_failed), Toast.LENGTH_SHORT).show();
        }else if(obj.getStatus() == 2){
            ip = dPM.GetWorkParaIP();
            port = String.valueOf(dPM.GetWorkParaPort());
//            user = dPM.GetWorkParaUserName();
//            pass = dPM.GetWorkParaPassword();
            mountPoint= dPM.GetWorkParaMountPoint();
            workMode = dPM.GetWorkParaMode();

            mIpEditText.setText(ip);
            mPorEditText.setText(port);
//            mUserEditText.setText(user);
//            mPasswdEditText.setText(pass);
            mAcceptTextView.setText(mountPoint);
            mModeTextView.setText(workMode);
        }
    }
    //连接返回消息
    public void onEventMainThread(NetConfigNtripEvent.DeviceNTRIPConnectEvent obj) {
        if (obj == null) return;
        if (obj.getStatus() == true) {
            Toast.makeText(this, getString(R.string.setting_rtk_net_connect_success), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, getString(R.string.setting_rtk_net_connect_failed), Toast.LENGTH_SHORT).show();
        }
    }
    //断开返回消息
    public void onEventMainThread(NetConfigNtripEvent.DeviceNTRIPDisConnectEvent obj) {
        if (obj == null) return;

        if (obj.getStatus() == true) {
            Toast.makeText(this, getString(R.string.setting_rtk_net_disconnect_success), Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this, getString(R.string.setting_rtk_net_disconnect_failed), Toast.LENGTH_SHORT).show();
        }
    }
}
